package com.assessment.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.assessment.entity.Comprehensive;
import com.assessment.entity.FirstPhase;
import com.assessment.entity.SecondPhase;
import com.assessment.entity.ThirdPhase;
import com.assessment.service.ComprehensiveService;
import com.assessment.service.Grade1Service;
import com.assessment.service.Grade2Service;
import com.assessment.service.Grade3Service;
import com.assessment.util.FileUpload;
import com.assessment.util.Grade1Total;
import com.assessment.util.ImportComprehensive;
import com.assessment.util.ImportFirstGrade;

@Controller
@RequestMapping("/find")
public class GradeController {
	@Resource
	private Grade1Service grade1Service;

	@Resource
	private Grade2Service grade2Service;

	@Resource
	private Grade3Service grade3Service;
	
	@Resource
	private ComprehensiveService comprehensiveService;
	//查询第一阶段的成绩列表
	@RequestMapping("/findFirstPhase.do")
	public String findFirstPhase(Model model){
		List<FirstPhase>firstPhaseList=grade1Service.findFirstPhase();
		model.addAttribute("firstPhaseList", firstPhaseList);
		Grade1Total g=new Grade1Total();
		for(FirstPhase firstPhase:firstPhaseList){
			while(firstPhase.getTotal()==null){
			g.grade1Total(firstPhase);
			grade1Service.updateTotal(firstPhase);
		}}
		
		return "Grade/grade1";
	
	}

	//查询第一阶段的成绩列表按成绩排序
	@RequestMapping("/findFirstPhaseBytotal.do")
	public String findFirstPhaseBytotal(Model model){
		List<FirstPhase>firstPhaseList=grade1Service.findFirstPhaseBytotal();
		model.addAttribute("firstPhaseList", firstPhaseList);
		
		return "Grade/grade1";
	
	}
	
	//根据员工号查询详细
	@RequestMapping(value="/findFirstPhaseExp.do" ,produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String findFirstPhaseExp(int staff_id){
		FirstPhase firstPhaseListExp=grade1Service.findFirstPhaseExp(staff_id);
		JSONObject object = JSONObject.fromObject(firstPhaseListExp);
		return object.toString();
	}

	
	
	
	
	
	
	
	//第二阶段成绩
	@RequestMapping("/findSecondPhase.do")
	public String findSecondPhase(Model model){
		List<SecondPhase> secondPhaseList=grade2Service.findSecondPhase();
		model.addAttribute("s2", secondPhaseList);
		//return "Grade/SecondPhase";
		return "Grade/grade2";
	}

	//第二阶段成绩按总成绩排序查询
	@RequestMapping("/findSecondPhaseBytotal.do")
	public String findSecondPhaseBytotal(Model model){
		List<SecondPhase> secondPhaseList=grade2Service.findSecondPhaseBytotal();
		System.out.println("++++++++++++++++");
		model.addAttribute("s2", secondPhaseList);
		//return "Grade/SecondPhase";
		return "Grade/grade2";
	}

	//根据工号查询第二阶段成绩
	@RequestMapping(value="/findSecondPhaseExp.do" ,produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String findSecondPhaseExp(int staff_id){
		SecondPhase secondPhaseListExp=grade2Service.findSecondPhaseExp(staff_id);
		JSONObject object = JSONObject.fromObject(secondPhaseListExp);
		return object.toString();
//		model.addAttribute("spexp", secondPhaseListExp);
//		return "Grade/ModifySecond";
//		return "grade2";
	}
	
	

	
	
	
	

	//修改第二阶段成绩
	@RequestMapping("/modifySecondGrade.do")
	public String modifySecondGrade(Model model,SecondPhase secondPhase){
System.out.println("++++++++++++++++++++++");
		grade2Service.modifySecondGrade(secondPhase);
		return "redirect:/find/findSecondPhase.do";
	}

	//第三阶段成绩
	@RequestMapping("/findThirdPhase.do")
	public String findThirdPhase(Model model){
		List<ThirdPhase>thirdPhaseList=grade3Service.findThirdPhase();
		model.addAttribute("thirdPhaseList", thirdPhaseList);
		return "Grade/grade3";
	}

	//第三阶段成绩按成绩排序
	@RequestMapping("/findThirdPhaseBytotal.do")
	public String findThirdPhaseBytotal(Model model){
		List<ThirdPhase>thirdPhaseList=grade3Service.findThirdPhaseBytotal();
		model.addAttribute("thirdPhaseList", thirdPhaseList);
		return "Grade/grade3";
	}
	
	
	//根据工号查询第三阶段个人信息
	@RequestMapping(value="/findThirdPhaseExp.do" ,produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String findThirdPhaseExp(int staff_id){
	
		ThirdPhase thirdPhaseListExp=grade3Service.findThirdPhaseExp(staff_id);
		JSONObject object = JSONObject.fromObject(thirdPhaseListExp);
		return object.toString();
//		return "Grade/ModifyThird";
	}
	//修改第三阶段成绩
	@RequestMapping("/modifyThirdGrade.do")
	public String modifyThirdGrade(Model model,ThirdPhase thirdPhase){
		System.out.println("-------------------");
		grade3Service.modifyThirdGrade(thirdPhase);
		return "redirect:/find/findThirdPhase.do";
	}

	@RequestMapping("/findThirdTeamGrade.do")
	public String findThirdTeamGrade(){
		return "Grade/ModifyThirdTeam";
	}

	//修改团队成绩
	@RequestMapping("/modifyTeamGrade.do")
	public String modifyTeamGrade(Model model,ThirdPhase thirdPhase){
		grade3Service.modifyTeamGrade(thirdPhase);
		return "redirect:/find/findThirdPhase.do";
	}
	
	//导入成绩明细
	@RequestMapping("/addGrade.do")
	public String add(FirstPhase grade1,Comprehensive filelujing,HttpServletRequest request) throws IOException{
			FileUpload upload=new FileUpload();
			String path=upload.fileupload(request);
			List <FirstPhase> list = new ArrayList<FirstPhase>();
			List <Comprehensive> list1= new ArrayList<Comprehensive>();
			ImportFirstGrade imfd=new ImportFirstGrade();
			ImportComprehensive imcs=new ImportComprehensive();
			list=imfd.importFirstGrade(path);
			list1=imcs.importCom(path);
			System.out.println(path);
			
			for(int i = 0 ; i < list.size() ; i++) {
				grade1Service.importFirstPhase(list.get(i));
	        	}
			for(int i = 0 ; i < list1.size() ; i++) {
				comprehensiveService.addCom(list1.get(i));
	        	}
			return "redirect:/find/findFirstPhase.do";
	}
}
